package me.mononin.factory.data.user;

import android.support.annotation.NonNull;

import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import java.util.LinkedList;
import java.util.List;

import me.mononin.common.factory.data.DataSource;
import me.mononin.factory.data.BaseDbRepository;
import me.mononin.factory.data.helper.DbHelper;
import me.mononin.factory.model.db.User;
import me.mononin.factory.model.db.User_Table;
import me.mononin.factory.persistence.Account;

/**
 * 联系人仓库
 */
public class ContractRepository extends BaseDbRepository<User> implements ContractDataSource{
    //用户缓存区,使用Set保证数据唯一性
    private List<User> users = new LinkedList<>();

    @Override
    public void load(DataSource.SucceedCallback<List<User>> callback) {
        super.load(callback);

        SQLite.select()
                .from(User.class)
                .where(User_Table.isFollow.eq(true))
                .and(User_Table.id.notEq(Account.getUserId()))
                .orderBy(User_Table.name, true)
                .limit(100)
                .async()
                .queryListResultCallback(this)
                .execute();
    }


    /**
     * 检查一个用户是否是我关注的用户
     * @param user 需要判断的用户
     * @return True为我已关注的用户
     */
    @Override
    protected boolean isRequired(User user) {
        return user.isFollow() && !user.getId().equals(Account.getUserId());
    }
}
